package com.qbb.server.aop.collection;

import org.apache.commons.beanutils.ConvertUtils;

import com.qbb.server.aop.enums.QbbConnsTypeEnum;

/**
 * 类说明
 * 
 * @author chenhuaiao
 * @version V1.0 创建时间：Mar 17, 2015 4:38:15 PM
 */
public class ConnsTypeUtil {
	// List接口保存的关联集合对象
	private static final String[] ListArray = new String[] { "java.util.List",
			"java.util.ArrayList", "java.util.LinkedList", "java.util.Stack",
			"java.util.Vector" };
	// Set接口保存的关联集合对象
	private static final String[] SetArray = new String[] { "java.util.Set",
			"java.util.HashSet", "java.util.TreeSet", "java.util.LinkedHashSet" };
	// Map接口保存的关联集合对象
	private static final String[] MapArray = new String[] { "java.util.Map",
			"java.util.HashMap", "java.util.Hashtable",
			"java.util.LinkedHashMap", "java.util.TreeMap",
			"java.util.concurrent.ConcurrentHashMap" };

	/**
	 * 获得集合定义的接口是List, or Set, or Map
	 * 
	 * @param classPath
	 * @return
	 */
	public static QbbConnsTypeEnum getType(String classPath) {
		int listLength = ListArray.length;
		for (int i = 0; i < listLength; i++) {
			if (ListArray[i].equals(classPath)) {
				return QbbConnsTypeEnum.List;
			}
		}

		int setLength = SetArray.length;
		for (int i = 0; i < setLength; i++) {
			if (SetArray[i].equals(classPath)) {
				return QbbConnsTypeEnum.Set;
			}
		}

		int maptLength = MapArray.length;
		for (int i = 0; i < maptLength; i++) {
			if (MapArray[i].equals(classPath)) {
				return QbbConnsTypeEnum.Map;
			}
		}
		return QbbConnsTypeEnum.OTHER;
	}

	/**
	 * 强转至指定类型的数据
	 * 
	 * @param fromClas
	 * @param toClas
	 * @param result
	 * @return
	 */
	public static Object parse(Class fromClas, Class toClas, Object result) {
		String fromSimpleName = fromClas.getSimpleName();
		String toSimpleName = toClas.getSimpleName();
		if (fromSimpleName.equals(toSimpleName)) {
			return result;
		}
		return ConvertUtils.convert(result, toClas);
	}
}
